import { getAjax, postAjax, deleteAjax, putAjax, download } from '@/utils/request'
import { resolveBlob } from '@/utils'
import { Mutation, Action } from 'vuex'

export interface GenTableColumn {
  columnId: number;
  tableId: number;
  columnName: string;
  columnComment: string;
  columnType: string;
  javaType: string;
  javaField: string;
  isPk: string;
  isIncrement: string;
  isRequired: string;
  isEdit: string;
  isList: string;
  isQuery: string;
  BaseQueryType: string;
  htmlType: string;
  dictType: string;
  sort: number;
  createBy: string;
  updateBy: string;
}

export interface GenTable {
  tableId: number | null;
  tableName?: string;
  tableComment?: string;
  subTableName?: string;
  subTableFkName?: string;
  className?: string;
  tplCategory?: string;
  packageName?: string;
  moduleName?: string;
  businessName?: string;
  functionName?: string;
  functionAuthor?: string;
  genType?: string;
  genPath?: string;
  options?: string;
  treeCode?: string;
  treeParentCode?: string;
  treeName?: string;
  parentMenuId?: number | null;
  parentMenuNam?: string;
  createBy?: string;
  updateBy?: string;
  beginTime?: Date;
  endTime?: Date;
  remark?: string;
  subTable?: GenTable;
  pkColumn?: GenTableColumn;
  columns?: Array<GenTableColumn>;
  parentMenuName?: string;
  selectParent?: Array<number> | null;
}

export interface StateType {
  info: GenTable;
  preview: Array<any>;
  tables: Array<GenTable>;
}

export interface ModuleType {
  namespaced: boolean;
  state: StateType;
  mutations: {
    SET_PREVIEW: Mutation<StateType>;
    SET_INFO: Mutation<StateType>;
    SET_TABLES: Mutation<StateType>;
  };
  actions: {
    getList: Action<StateType, StateType>;
    updateGenTable: Action<StateType, StateType>;
    getGenTable: Action<StateType, StateType>;
    getDBList: Action<StateType, StateType>;
    importTable: Action<StateType, StateType>;
    delete: Action<StateType, StateType>;
    synchDb: Action<StateType, StateType>;
    previewTable: Action<StateType, StateType>;
    genCode: Action<StateType, StateType>;
    downLoadZip: Action<StateType, StateType>;
  };
}

const storeModel: ModuleType = {
  namespaced: true,
  state: {
    info: {
      tableId: null,
      tableName: '',
      tableComment: '',
      subTableName: '',
      subTableFkName: '',
      className: '',
      tplCategory: '',
      packageName: '',
      moduleName: '',
      businessName: '',
      functionName: '',
      functionAuthor: '',
      genType: '',
      genPath: '',
      options: '',
      treeCode: '',
      treeParentCode: '',
      treeName: '',
      parentMenuId: null,
      parentMenuName: '',
      createBy: '',
      updateBy: '',
      beginTime: new Date(),
      endTime: new Date(),
      remark: '',
      columns: [],
      selectParent: null
    },
    preview: [],
    tables: []
  },
  mutations: {
    SET_PREVIEW: (state, data) => {
      state.preview = data
    },
    SET_INFO: (state, data) => {
      state.info = data
    },
    SET_TABLES: (state, data) => {
      state.tables = data
    }
  },
  actions: {
    getList(_, data) {
      return getAjax('/tool/gen/list', data)
    },
    updateGenTable(_, data) {
      return putAjax('/tool/gen/', data)
    },
    getGenTable(_, id) {
      return getAjax(`/tool/gen/${id}`, null)
    },
    getDBList(_, data) {
      return getAjax('/tool/gen/db/list', data)
    },
    importTable(_, list) {
      return postAjax('/tool/gen/importTable', { tables: list })
    },
    delete(_, ids) {
      return deleteAjax(`/tool/gen/${ids}`, null)
    },
    synchDb(_, tableName) {
      return getAjax(`/tool/gen/synchDb/${tableName}`, null)
    },
    // 预览生成代码
    previewTable({ commit }, tableId) {
      return new Promise((resolve, reject) => {
        getAjax(`/tool/gen/preview/${tableId}`, null).then((res: any) => {
          commit('SET_PREVIEW', res.data)
          resolve(res)
        }).catch(err => { reject(err) })
      })
    },
    genCode(_, tableName) {
      return getAjax(`/tool/gen/genCode/${tableName}`, null)
    },
    downLoadZip(_, tableNames) {
      return new Promise((resolve, reject) => {
        download('tool/gen/batchGenCode?tables=' + tableNames).then(res => {
          resolveBlob(res, 'zip')
          resolve(res)
        }).catch(err => { reject(err) })
      })
    }
  }
}

export default storeModel
